package com.jmx;

import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;

import org.springframework.jmx.export.annotation.ManagedAttribute;
import org.springframework.jmx.export.annotation.ManagedOperation;
import org.springframework.jmx.export.annotation.ManagedResource;

@ManagedResource(objectName = "com.jmx:name=IJmxBean", description = "My Managed Bean", log = true, logFile = "jmx.log", currencyTimeLimit = 15, persistPolicy = "OnUpdate", persistPeriod = 200, persistLocation = "foo", persistName = "bar")
public class JmxBean implements IJmxBean {

	@Override
	@ManagedOperation(description="show name and age.")
	public String getPersion() {
		// TODO Auto-generated method stub
		return "name is " + this.name + " and age is " + this.age;
	}
	
	@Override
	@ManagedOperation(description="show memory info.")
	public long getMemoryInfo() {
		MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
		return memoryMXBean.getHeapMemoryUsage().getUsed();
		
	}
	
	private String name;
	private int age;

	@ManagedAttribute(description = "The Age Attribute", currencyTimeLimit = 15)
	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	@ManagedAttribute(description = "The Name Attribute", currencyTimeLimit = 20, defaultValue = "bar", persistPolicy = "OnUpdate")
	public void setName(String name) {
		this.name = name;
	}

	@ManagedAttribute(defaultValue = "foo", persistPeriod = 300)
	public String getName() {
		return name;
	}

}
